/**
 * $Id$
 * @author mcanovas
 * @date   Mar 1, 2012 12:32:30 PM
 *
 * Copyright (C) 2012 Scytl Secure Electronic Voting SA
 *
 * All rights reserved.
 *
 */
package org.sk.gsync.ui.configuration;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;

import org.sk.gsync.app.mybatis.Folder;
import org.sk.gsync.ui.DefaultFrame;
import org.sk.gsync.ui.SaveCancelButtonsPanel;
import org.sk.gsync.ui.context.AbstractContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class ConfigurationFrame extends DefaultFrame {

    private static final long serialVersionUID = -7121873165168512538L;

    @Autowired
    private SelectPathPanel selectPathPanel;

    @Autowired
    private SaveCancelButtonsPanel saveCancelButtonsPanel;

    /**
     * @see org.sk.gsync.ui.DefaultFrame#configure()
     */
    @Override
    public void configure() {
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = screenSize.width / 2;
        int height = screenSize.height / 2;
        setSize(width, height);
        setPreferredSize(new Dimension(width, height));

        // Configure selected path panel context
        selectPathPanel.getContext().setValue(
            ConfigurationFrameFields.FOLDERS_TO_SYNC,
            getContext()
                .getValue(ConfigurationFrameFields.FOLDERS_TO_SYNC));
        selectPathPanel.refresh();

        // add select path panel to parent.
        add(selectPathPanel, BorderLayout.PAGE_START);

        // configure buttons panel
        saveCancelButtonsPanel.addSaveAction(new ActionListener() {

            public void actionPerformed(final ActionEvent e) {
                @SuppressWarnings("unchecked")
                List<Folder> folders =
                    (List<Folder>) selectPathPanel.getContext().getValue(
                        ConfigurationFrameFields.FOLDERS_TO_SYNC);
                ConfigurationFrameContext context =
                    (ConfigurationFrameContext) getContext();
                context.setValue(ConfigurationFrameFields.FOLDERS_TO_SYNC,
                    folders);
                context.save();
                dispose();
            }
        });
        saveCancelButtonsPanel.addCancelAction(new ActionListener() {

            public void actionPerformed(final ActionEvent e) {
                dispose();
            }
        });
        add(saveCancelButtonsPanel, BorderLayout.SOUTH);
    }

    /**
     * @see org.sk.gsync.ui.DefaultFrame#updateContext()
     */
    @Override
    public void updateContext() {
    }

    /**
     * @see org.sk.gsync.ui.DefaultFrame#setContext(org.sk.gsync.ui.context.AbstractContext)
     */
    @Override
    @Autowired
    @Qualifier("configurationFrameContext")
    protected void setContext(final AbstractContext context) {
        super.setContext(context);
    }

}
